pkgname=grobi
pkgver=0.6.0
pkgrel=3
pkgdesc='Automatically configure monitors/outputs for Xorg via RANDR'
arch=('loong64' 'x86_64')
url='https://github.com/fd0/grobi'
license=('BSD')
makedepends=('go')
source=("$pkgname-$pkgver.tar.gz::https://github.com/fd0/grobi/archive/refs/tags/v$pkgver.tar.gz"
"$pkgname-fix-test.patch::https://github.com/fd0/grobi/commit/176988ab087ff92d1408fbc454c77263457f3d7e.patch"
"$pkgname-fix-edid-junk.patch::https://github.com/fd0/grobi/pull/33.patch"
)
sha256sums=('846cc199ca9e5a223b923f517e4f1c42e9f78553fe12b83788c327083b95c519'
            'd549e83b067540d15d56d93350c3561c74f91132bdf63cde8fd8aeab5bcb8176'
            'd34f78816fa0a5b48fc0e5fe68d27087311768f9759dfdd7af82ff23b31acf7e')

prepare(){
  cd "$pkgname-$pkgver"
  mkdir -p build/

  patch -Np1 < "$srcdir/grobi-fix-test.patch"
  patch -Np1 < "$srcdir/grobi-fix-edid-junk.patch"
}

build() {
  cd "$pkgname-$pkgver"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"
  go build -o build .
}

check() {
  cd "$pkgname-$pkgver"
  go test ./...
}

package() {
  cd "$pkgname-$pkgver"
  install -Dm755 build/$pkgname "$pkgdir"/usr/bin/$pkgname
  install -Dm644 doc/grobi.service "$pkgdir"/usr/lib/systemd/user/$pkgname.service
}
